 /* frmManutencaoDeUsuarios.java
 *
 * Created on 08/11/2011, 10:37:19
 */
package scc.Telas;

import Dao.UserDAO;
import Entidade.Usuario;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Davidson
 */
public class frmManutencaoDeUsuarios extends javax.swing.JInternalFrame {

    private static frmManutencaoDeUsuarios tela1;
    private static Usuario usuario;
    private static UserDAO dao;
    
    /** Creates new form frmManutencaoDeUsuarios */
    private frmManutencaoDeUsuarios() {
        initComponents();        
        lblUsuario.setText(usuario.getNome());        
        this.setVisible(true);
        this.moveToFront();        
    }
    
    public static frmManutencaoDeUsuarios getInstancia(UserDAO dao2, Usuario usuario2){
        if (tela1 == null) {
            dao = dao2;
            usuario = usuario2;
            tela1 = new frmManutencaoDeUsuarios();
        }
        return tela1;
    }    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblUsuario = new javax.swing.JLabel();
        lblInfo = new javax.swing.JLabel();
        lblSenhaAtual = new javax.swing.JLabel();
        pwdSenhaAtual = new javax.swing.JPasswordField();
        lblNovaSenha = new javax.swing.JLabel();
        pwdNovaSenha = new javax.swing.JPasswordField();
        lblNovaSenhaConfirmacao = new javax.swing.JLabel();
        pwdNovaSenhaConfirmacao = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        lblTitulo1 = new javax.swing.JLabel();
        btAlterarSenha = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setMinimumSize(new java.awt.Dimension(758, 361));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblUsuario.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        lblUsuario.setForeground(new java.awt.Color(102, 102, 102));

        lblInfo.setText("O Sistema diferencia entre maiúsculo e minúsculo.");

        lblSenhaAtual.setText("Digite sua senha atual");

        pwdSenhaAtual.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        lblNovaSenha.setText("Digite sua senha atual");

        pwdNovaSenha.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        lblNovaSenhaConfirmacao.setText("Confirme sua nova senha");

        pwdNovaSenhaConfirmacao.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/scc/Imagens/User.png"))); // NOI18N

        lblTitulo1.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        lblTitulo1.setForeground(new java.awt.Color(0, 51, 153));
        lblTitulo1.setText("Altere sua senha");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(lblUsuario))
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblNovaSenhaConfirmacao)
                    .addComponent(pwdNovaSenhaConfirmacao, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNovaSenha)
                    .addComponent(pwdNovaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSenhaAtual)
                    .addComponent(lblTitulo1)
                    .addComponent(pwdSenhaAtual, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(323, 323, 323))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblTitulo1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblSenhaAtual, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pwdSenhaAtual)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblNovaSenha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pwdNovaSenha)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblNovaSenhaConfirmacao, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pwdNovaSenhaConfirmacao)
                        .addGap(18, 18, 18)
                        .addComponent(lblInfo)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblUsuario)
                        .addGap(70, 70, 70))))
        );

        btAlterarSenha.setText("Alterar Senha");
        btAlterarSenha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarSenhaActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(402, 402, 402)
                .addComponent(btAlterarSenha)
                .addGap(18, 18, 18)
                .addComponent(btCancelar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE, false)
                    .addComponent(btCancelar)
                    .addComponent(btAlterarSenha))
                .addGap(66, 66, 66))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        
        setVisible(false);        
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btAlterarSenhaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarSenhaActionPerformed
        
        String senhaAtual = new String(pwdSenhaAtual.getPassword());
        String novaSenha = new String(pwdNovaSenha.getPassword());
        String novaSenhaConfirmacao = new String(pwdNovaSenhaConfirmacao.getPassword());
        
        if (senhaAtual.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Digite a senha atual!", "Manutenção de Usuário", JOptionPane.ERROR_MESSAGE);
            pwdSenhaAtual.requestFocus();
            return;
        } else if (novaSenha.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Digite a nova senha!", "Manutenção de Usuário", JOptionPane.ERROR_MESSAGE);
            pwdNovaSenha.requestFocus();            
            return;
        } else if (novaSenhaConfirmacao.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Confirme a nova senha!", "Manutenção de Usuário", JOptionPane.ERROR_MESSAGE);
            pwdNovaSenhaConfirmacao.requestFocus();
            return;
        }        
        if (!novaSenha.equals(novaSenhaConfirmacao)) {
            JOptionPane.showMessageDialog(rootPane, "A nova senha não confere com a senha do campo confirmação!", "Manutenção de Usuário", JOptionPane.ERROR_MESSAGE);
            pwdNovaSenha.setText("");
            pwdNovaSenhaConfirmacao.setText("");
            pwdNovaSenha.requestFocus();
            return;
        }        
        try {
            if (dao.validaLogon(usuario.getLogin(), senhaAtual)) {
                dao.alteraSenha(usuario.getLogin(), novaSenha);
                JOptionPane.showMessageDialog(rootPane, "Senha Alterada!");
                pwdNovaSenha.setText("");
                pwdNovaSenhaConfirmacao.setText("");
                pwdSenhaAtual.setText("");
            } else {
                JOptionPane.showMessageDialog(rootPane, "A senha atual está incorreta!", "Manutenção de Usuário", JOptionPane.ERROR_MESSAGE);            
                pwdSenhaAtual.setText("");
                pwdSenhaAtual.requestFocus();
                return;
            }
        } catch (SQLException ex) {
            Logger.getLogger(frmManutencaoDeUsuarios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btAlterarSenhaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAlterarSenha;
    private javax.swing.JButton btCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblNovaSenha;
    private javax.swing.JLabel lblNovaSenhaConfirmacao;
    private javax.swing.JLabel lblSenhaAtual;
    private javax.swing.JLabel lblTitulo1;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JPasswordField pwdNovaSenha;
    private javax.swing.JPasswordField pwdNovaSenhaConfirmacao;
    private javax.swing.JPasswordField pwdSenhaAtual;
    // End of variables declaration//GEN-END:variables
}
